package photostock.view;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.filter.ElementFilter;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class WebPage {

	public static final long serialVersionUID = 20090501L;
	private Document doc = null;
	
	public WebPage(Document d) {
		doc = d;
	}
	
	public String getDocType() {
		return doc.getDocType().toString();
	}

	public Element getHead() {
		
		return doc.getDescendants(new ElementFilter("head")).hasNext() ? (Element)doc.getDescendants(new ElementFilter("head")).next() : null;
	}

	public Element getBody() {
		
		return doc.getDescendants(new ElementFilter("body")).hasNext() ? (Element)doc.getDescendants(new ElementFilter("body")).next() : null;
	}

	
	public Element getElement(String s) {
		
		return doc.getRootElement().getDescendants(WidgetFactory.getIdFilter(s)).hasNext() ? 
				(Element)doc.getRootElement().getDescendants(WidgetFactory.getIdFilter(s)).next() : null;
	}

	public String toString() {
        XMLOutputter output = new XMLOutputter(Format.getPrettyFormat().setOmitDeclaration(true));
        return output.outputString(doc);
	}
	
}
